package com.xpgk.plugin;

import java.text.MessageFormat;
import java.util.List;

import com.jfinal.log.Log;
import com.jfinal.plugin.IPlugin;
import com.xpgk.constant.ConstantInit;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.DataBase;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.deptStation.DeptStation;
import com.xpgk.mvc.deptStation.DeptStationService;
import com.xpgk.mvc.right.Right;
import com.xpgk.mvc.user.User;
import com.xpgk.tools.ToolCache;
import com.xpgk.tools.ToolDataBase;
import com.xpgk.tools.ToolSqlXml;

/**
 * 系统初始化缓存操作类
 */
public class ParamInitPlugin implements IPlugin {

	private static final Log log = Log.getLog(ParamInitPlugin.class);

	/**
	 * 数据批处理大小，每批次处理一万行
	 */
	protected static final int splitDataSize = 10000;

	/**
	 * 用户缓存key前缀
	 */
	public static String cacheStart_user = "user_";


	/**
	 * 岗位功能缓存key前缀
	 */
	public static String cacheStart_station_right = "station_right_";

	/**
	 * 功能缓存key前缀
	 */
	public static String cacheStart_right = "right_";

	/**
	 * 字典缓存key前缀
	 */
	public static String cacheStart_dict = "dict_";

	/**
	 * 字典子节点缓存key前缀
	 */
	public static String cacheStart_dict_child = "dict_child_";

	/**
	 * 参数缓存key前缀
	 */
	public static String cacheStart_code = "code_";

	/**
	 * 参数子节点缓存key前缀
	 */
	public static String cacheStart_code_child = "code_child_";
	
	

	@Override
	public boolean start() {
		log.info("缓存参数初始化 start ...");

		// 1.缓存用户
		platform_cacheUser();

		//3.缓存岗位权限
		platform_cacheStationOperator();

		// 4.缓存权限
		platform_cacheRight();

		// 5.缓存字典
		// platform_cacheDict();

		// 6.缓存参数
		 platform_cacheCode();

		log.info("缓存参数初始化 end ...");
		return true;
	}

	@Override
	public boolean stop() {
		return true;
	}

	/**
	 * 缓存所有用户
	 */
	public static void platform_cacheUser() {
		log.info("缓存加载：User start");

		String sql = ToolSqlXml.getSqlMy(User.sqlId_paging, null, ConstantRender.sql_renderType_beetl);
		long batchCount = BaseService.getBatchCount(" from " + User.table_name, splitDataSize);
		List<User> userList = null;
		for (long i = 0; i < batchCount; i++) {
			userList = User.dao.find(sql, splitDataSize, i * splitDataSize); // start
			for (User user : userList) {
				User.cacheAdd(user.getPKValue());
			}
			userList = null;
		}
		log.info("缓存加载：User end");
	}

	/**
	 * 缓存所有的岗位权限
	 */
	public static void platform_cacheStationOperator() {
		
		String sql = ToolSqlXml.getSqlMy(DeptStation.sqlId_paging, null, ConstantRender.sql_renderType_beetl);
		
		long batchCount = BaseService.getBatchCount(" from " + DeptStation.table_name, splitDataSize);
		List<DeptStation> stationList = null;
		for (long i = 0; i < batchCount; i++) {
			stationList = DeptStation.dao.find(sql, splitDataSize, i * splitDataSize);
			
			for (DeptStation station : stationList) {
				DeptStationService.cacheAdd(station.getPKValue());
			}
			stationList = null;
		}
		
		log.info("缓存加载：Station Operator end");
	}

	/**
	 * 缓存权限
	 */
	public static void platform_cacheRight() {

		log.info("缓存加载：Right start");
		String sql = ToolSqlXml.getSqlMy(Right.sqlId_paging, null, ConstantRender.sql_renderType_beetl);
		long batchCount = BaseService.getBatchCount( " from " + Right.table_name, splitDataSize);
		List<Right> rightList = null;
		for (long i = 0; i < batchCount; i++) {
			rightList = Right.dao.find(sql, splitDataSize, i * splitDataSize);
			for (Right right : rightList) {
				ToolCache.set(ParamInitPlugin.cacheStart_right + right.getPKValue(), right);
				ToolCache.set(ParamInitPlugin.cacheStart_right + right.getStr(Right.column_uri), right);
			}
			rightList = null;
		}

		log.info("缓存加载：Right end");
	}

	/**
	 * 缓存业务字典
	 */
	// public static void platform_cacheDict() {
	// if(log.isInfoEnabled()) log.info("缓存加载：Dict start");
	// String sql = ToolSqlXml.getSqlMy(Dict.sqlId_all);
	// List<Dict> dictList = Dict.dao.find(sql);
	// for (Dict dict : dictList) {
	// Dict.dao.cacheAdd(dict.getPKValue());
	// dict = null;
	// }
	// if(log.isInfoEnabled()) log.info("缓存加载：Dict end, size = " +
	// dictList.size());
	// dictList = null;
	// }

	/**
	 * 缓存业务参数
	 */
	 public static void platform_cacheCode() {
		 String sql = ToolSqlXml.getSqlMy(Code.sqlId_all);
		 List<Code> codeList = Code.dao.find(sql);
		 for (Code code : codeList) {
			 Code.cacheAdd(code.getPKValue());
			 code = null;
		 }
		 codeList = null;
	 }

}
